/**
 * Created by Administrator on 2017/5/31 0031.
 */
$(function () {
    //用户id
    var uid = $.cookie("userid");
    $.ajax({
        url:"./server/cartlist.php",
        data:{
            uid:uid
        },
        type:"post",
        success:function (res) {

            var arr = JSON.parse(res);
            var o =0;
            var O = $(".O");
            for (var i = 0; i < arr.length; i++){
                o+= parseInt(arr[i].carnumber);
            }
            O.eq(0).text(o);
            O.eq(2).text(o);
            O.eq(1).html("<s></s>"+ o);
            //总价;
            var totol = Number(arr[0].goprice1)*parseInt(arr[0].carnumber);
            $(".total-num").html("￥"+totol);
            //需要添加内容的父级；
            var parent = $("#ping");
            var str = arr[0].goprice;
            var tempt = "";
            tempt+='<div class="col-md-1">';
                tempt += '<input type="checkbox" class="select" checked value="goid">';
                tempt+= '</div>';
                tempt+='<div class="col-md-3">';
                tempt+='<div class="g-pic">';
            tempt+='<img src="'+ arr[0].goSimg +'" alt="">';
                tempt+='</div>';
                tempt+='<div class="g-info">';
                tempt+='<p>';
                tempt+='<a href="products.html">'+ arr[0].goname +'</a>';
            tempt+='<br>';
            tempt+='商品编码: '+ arr[0].goid;
            tempt+='</p>';
            tempt+='<p class="ts_g">此商品在中国有售</p>';
            tempt+='</div>';
            tempt+='</div>';
            tempt+='<div class="col-md-1 mt-10"></div>';
            tempt+='<div class="col-md-2 center mt-10">';
            tempt+='<b>'+ arr[0].goprice +'</b>';
            tempt+='</div>';
            tempt+='<div class="col-md-2 center mt-10">';
            tempt+='<div class="spin clear center">';
            tempt+='<a class="jian" href="javascript:void (0)"><i>-</i></a>';
            tempt+='<input value="'+ arr[0].carnumber +'" class="count" data-goid="'+ arr[0].goid +'" data-goprice="'+ Number(arr[0].goprice1) +'" data-goname="'+ arr[0].goname +'">';
            tempt+='<a class="jia" href="javascript:void (0)"><i>+</i></a>';
            tempt+='</div>';
            tempt+='</div>';
            tempt+='<div class="col-md-2 center mt-10">';
            tempt+='<b class="total">'+ Number(arr[0].goprice1)*parseInt(arr[0].carnumber) +'</b>';
            console.log(Number(arr[0].goprice1));
            console.log(str);
            tempt+='</div>';
            tempt+='<div class="col-md-1 center mt-10">';
            tempt+='<a href="javascript:void (0)">';
            tempt+='<s class="del"></s>';
            tempt+='</a>';
            tempt+='</div>';
            parent.html(parent.html()+tempt);
        }
    });
    var body = $("body")
    body.on("click",".jia",function () {
        $(this).prev("input").val(parseInt($(this).prev("input").val())+1);
        var val = $(this).prev("input").val();
        var goid = $(this).prev("input").data("goid");
        var goprice = $(this).prev("input").data("goprice");
        var goname = $(this).prev("input").data("goname");
        var O = $(".O");
        var total = val * goprice;
        $(".total").html("￥"+total.toFixed(2));
        $(".total-num").html("￥"+total.toFixed(2));
        O.eq(0).text(val);
        O.eq(2).text(val);
        O.eq(1).html("<s></s>"+ val);
        $.ajax({
            url:"./server/jiajian.php",
            data:{
                goid:goid,
                num:val,
                goprice:goprice,
                goname:goname,
                uid:uid
            },
            type:"post"
        })
    });
    body.on("click",".jian",function () {
        if($(this).next("input").val()>1){
            $(this).next("input").val(parseInt($(this).next("input").val())-1);
        }
        var val = $(this).next("input").val();
        var goid = $(this).next("input").data("goid");
        var goprice = $(this).next("input").data("goprice");
        var goname = $(this).next("input").data("goname");
        var O = $(".O");
        O.eq(0).text(val);
        O.eq(2).text(val);
        O.eq(1).html("<s></s>"+ val);
        var total = val * goprice;
        console.log(total.toFixed(2));
        $(".total").html("￥"+total.toFixed(2));
        $(".total-num").html("￥"+total.toFixed(2));
        $.ajax({
            url:"./server/jiajian.php",
            data:{
                goid:goid,
                num:val,
                goprice:goprice,
                goname:goname,
                uid:uid
            },
            type:"post"
        })
    })

});